package cn.iocoder.yudao.module.trade.service.lifecircle;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.number.NumberUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.module.trade.controller.admin.lifecircle.vo.*;
import cn.iocoder.yudao.module.trade.controller.app.lifecircle.vo.AppLifeCircleStoreRespVO;
import cn.iocoder.yudao.module.trade.controller.app.lifecircle.vo.AppLifeCircleStoreTypeRespVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.lifecircle.LifeCircleStoreDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.lifecircle.LifeCircleStorePointsDO;
import cn.iocoder.yudao.module.trade.dal.mysql.lifecircle.LifeCircleStoreMapper;
import cn.iocoder.yudao.module.trade.dal.mysql.lifecircle.LifeCircleStorePointsMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.LIFE_CIRCLE_STORE_NOT_EXISTS;

/**
 * 自提门店 Service 实现类
 *
 * @author jason
 */
@Service
@Validated
public class LifeCircleStoreServiceImpl implements LifeCircleStoreService {

    @Resource
    private LifeCircleStoreMapper lifeCircleStoreMapper;

    @Resource
    private LifeCircleStorePointsMapper lifeCircleStorePointsMapper;

    @Override
    public Long createLifeCircleStore(LifeCircleStoreCreateReqVO createReqVO) {
        // 插入
        LifeCircleStoreDO lifeCircleStoreDO = this.convert(createReqVO);

        lifeCircleStoreMapper.insert(lifeCircleStoreDO);
        // 返回
        return lifeCircleStoreDO.getId();
    }

    @Override
    public void updateLifeCircleStore(LifeCircleStoreUpdateReqVO updateReqVO) {
        // 校验存在
        validateLifeCircleStoreExists(updateReqVO.getId());
        // 更新
        LifeCircleStoreDO updateObj = this.convert(updateReqVO);
        lifeCircleStoreMapper.updateById(updateObj);
    }

    @Override
    public void deleteLifeCircleStore(Long id) {
        // 校验存在
        validateLifeCircleStoreExists(id);
        // 删除
        lifeCircleStoreMapper.deleteById(id);
    }

    private void validateLifeCircleStoreExists(Long id) {
        LifeCircleStoreDO lifeCircleStore = lifeCircleStoreMapper.selectById(id);
        if (lifeCircleStore == null) {
            throw exception(LIFE_CIRCLE_STORE_NOT_EXISTS);
        }
    }

    @Override
    public LifeCircleStoreDO getLifeCircleStore(Long id) {
        return lifeCircleStoreMapper.selectById(id);
    }

    @Override
    public List<LifeCircleStoreRespVO> getLifeCircleStoreList(Collection<Long> ids) {
        List<LifeCircleStoreDO> lifeCircleStoreDOList = lifeCircleStoreMapper.selectBatchIds(ids);
        List<LifeCircleStoreRespVO> res = new ArrayList<>(lifeCircleStoreDOList.size());
        lifeCircleStoreDOList.stream().forEach(lifeCircleStoreDO -> {
            res.add(lifeCircleStoreDOToLifeCircleStoreRespVO(lifeCircleStoreDO));
        });
        return res;
    }

    protected LifeCircleStoreRespVO lifeCircleStoreDOToLifeCircleStoreRespVO(LifeCircleStoreDO lifeCircleStoreDO) {
        if (lifeCircleStoreDO == null) {
            return null;
        }
        LifeCircleStoreRespVO lifeCircleStoreRespVO = new LifeCircleStoreRespVO();

        lifeCircleStoreRespVO.setName(lifeCircleStoreDO.getName());
        lifeCircleStoreRespVO.setIntroduction(lifeCircleStoreDO.getIntroduction());
        lifeCircleStoreRespVO.setPhone(lifeCircleStoreDO.getPhone());
        lifeCircleStoreRespVO.setAreaId(lifeCircleStoreDO.getAreaId());
        lifeCircleStoreRespVO.setDetailAddress(lifeCircleStoreDO.getDetailAddress());
        lifeCircleStoreRespVO.setLogo(lifeCircleStoreDO.getLogo());
        lifeCircleStoreRespVO.setStoreImages(lifeCircleStoreDO.getStoreImages());
        lifeCircleStoreRespVO.setOpeningTime(lifeCircleStoreDO.getOpeningTime());
        lifeCircleStoreRespVO.setClosingTime(lifeCircleStoreDO.getClosingTime());
        lifeCircleStoreRespVO.setLatitude(lifeCircleStoreDO.getLatitude());
        lifeCircleStoreRespVO.setLongitude(lifeCircleStoreDO.getLongitude());
        lifeCircleStoreRespVO.setStatus(lifeCircleStoreDO.getStatus());
        lifeCircleStoreRespVO.setId(lifeCircleStoreDO.getId());
        lifeCircleStoreRespVO.setCreateTime(lifeCircleStoreDO.getCreateTime());
        lifeCircleStoreRespVO.setType(lifeCircleStoreDO.getType());

        if (null != lifeCircleStoreDO.getPointsIds()) {
            lifeCircleStoreRespVO.setPoints(lifeCircleStorePointsMapper.selectByPointsIds(lifeCircleStoreDO.getPointsIds()));
        }

        return lifeCircleStoreRespVO;
    }


    @Override
    public List<LifeCircleStoreDO> getLifeCircleStoreListByStatus(Integer status) {
        return lifeCircleStoreMapper.selectListByStatus(status);
    }

    @Override
    public PageResult<LifeCircleStoreRespVO> getLifeCircleStorePage(LifeCircleStorePageReqVO pageReqVO) {
        PageResult<LifeCircleStoreDO> lifeCircleStoreDOPageResult = lifeCircleStoreMapper.selectPage(pageReqVO);
        List<LifeCircleStoreRespVO> lifeCircleStoreRespVOS = CollectionUtils.convertList(lifeCircleStoreDOPageResult.getList(), this::lifeCircleStoreDOToLifeCircleStoreRespVO);
        return new PageResult<>(lifeCircleStoreRespVOS, lifeCircleStoreDOPageResult.getTotal());
    }

    @Override
    public void bindLifeCircleStore(LifeCircleBindReqVO bindReqVO) {
        // 1.1 校验门店存在
        validateLifeCircleStoreExists(bindReqVO.getId());
        // 1.2 校验积分存在
//        adminUserApi.validateUserList(bindReqVO.getPointsIds()).checkError();

        // 2. 更新
        LifeCircleStoreDO updateObj = BeanUtils.toBean(bindReqVO, LifeCircleStoreDO.class);
        lifeCircleStoreMapper.updateById(updateObj);
    }

    @Override
    public List<LifeCircleStorePointsVO> getPointsListByIds(List<Long> ids) {
        return lifeCircleStorePointsMapper.selectByPointsIds(ids);
    }

    @Override
    public List<AppLifeCircleStoreRespVO> getLifeCircleStoreListApp(Double latitude, Double longitude) {
        List<LifeCircleStoreDO> lifeCircleStoreDOS = lifeCircleStoreMapper.selectListByStatus(null);
        List<AppLifeCircleStoreRespVO> res = CollectionUtils.convertList(lifeCircleStoreDOS, store -> {
            AppLifeCircleStoreRespVO storeVO = convert03(store);
            if (latitude != null && longitude != null) {
                storeVO.setDistance(NumberUtils.getDistance(latitude, longitude, storeVO.getLatitude(), storeVO.getLongitude()));
            }
            storeVO.setStatus(store.getStatus());
            if (null != store.getPointsIds()) {
                storeVO.setPoints(lifeCircleStorePointsMapper.selectByPointsIds(store.getPointsIds()));
            }
            storeVO.setStoreImages(store.getStoreImages());
            storeVO.setType(store.getType());
            return storeVO;
        });
        return res;
    }

    @Override
    public PageResult<LifeCircleStorePointsDO> getPointsList(LifeCircleStorePointsPageReqVO reqVO) {
        PageResult<LifeCircleStorePointsDO> list = lifeCircleStorePointsMapper.selectPage(reqVO);
        return list;
    }

    @Override
    public List<AppLifeCircleStoreTypeRespVO> getTypeList() {
        return lifeCircleStorePointsMapper.getTypeList();
    }

    public AppLifeCircleStoreRespVO convert03(LifeCircleStoreDO bean) {
        if (bean == null) {
            return null;
        }

        AppLifeCircleStoreRespVO appLifeCircleStoreRespVO = new AppLifeCircleStoreRespVO();

        appLifeCircleStoreRespVO.setAreaName(AreaUtils.format(bean.getAreaId()));
        appLifeCircleStoreRespVO.setId(bean.getId());
        appLifeCircleStoreRespVO.setName(bean.getName());
        appLifeCircleStoreRespVO.setLogo(bean.getLogo());
        appLifeCircleStoreRespVO.setPhone(bean.getPhone());
        appLifeCircleStoreRespVO.setAreaId(bean.getAreaId());
        appLifeCircleStoreRespVO.setDetailAddress(bean.getDetailAddress());
        appLifeCircleStoreRespVO.setLatitude(bean.getLatitude());
        appLifeCircleStoreRespVO.setLongitude(bean.getLongitude());
        appLifeCircleStoreRespVO.setStatus(bean.getStatus());

        return appLifeCircleStoreRespVO;
    }

    public LifeCircleStoreDO convert(LifeCircleStoreCreateReqVO bean) {
        if (bean == null) {
            return null;
        }

        LifeCircleStoreDO lifeCircleStoreDO = new LifeCircleStoreDO();

        lifeCircleStoreDO.setName(bean.getName());
        lifeCircleStoreDO.setIntroduction(bean.getIntroduction());
        lifeCircleStoreDO.setPhone(bean.getPhone());
        lifeCircleStoreDO.setAreaId(bean.getAreaId());
        lifeCircleStoreDO.setDetailAddress(bean.getDetailAddress());
        lifeCircleStoreDO.setLogo(bean.getLogo());
        lifeCircleStoreDO.setOpeningTime(bean.getOpeningTime());
        lifeCircleStoreDO.setClosingTime(bean.getClosingTime());
        lifeCircleStoreDO.setLatitude(bean.getLatitude());
        lifeCircleStoreDO.setLongitude(bean.getLongitude());
        lifeCircleStoreDO.setStatus(bean.getStatus());
        lifeCircleStoreDO.setStoreImages(bean.getStoreImages());
        lifeCircleStoreDO.setDetailImage(bean.getDetailImage());
        lifeCircleStoreDO.setType(bean.getType());
        return lifeCircleStoreDO;
    }

    public LifeCircleStoreDO convert(LifeCircleStoreUpdateReqVO bean) {
        if (bean == null) {
            return null;
        }

        LifeCircleStoreDO lifeCircleStoreDO = new LifeCircleStoreDO();

        lifeCircleStoreDO.setId(bean.getId());
        lifeCircleStoreDO.setName(bean.getName());
        lifeCircleStoreDO.setIntroduction(bean.getIntroduction());
        lifeCircleStoreDO.setPhone(bean.getPhone());
        lifeCircleStoreDO.setAreaId(bean.getAreaId());
        lifeCircleStoreDO.setDetailAddress(bean.getDetailAddress());
        lifeCircleStoreDO.setLogo(bean.getLogo());
        lifeCircleStoreDO.setOpeningTime(bean.getOpeningTime());
        lifeCircleStoreDO.setClosingTime(bean.getClosingTime());
        lifeCircleStoreDO.setLatitude(bean.getLatitude());
        lifeCircleStoreDO.setLongitude(bean.getLongitude());
        lifeCircleStoreDO.setStatus(bean.getStatus());
        lifeCircleStoreDO.setStoreImages(bean.getStoreImages());
        lifeCircleStoreDO.setDetailImage(bean.getDetailImage());
        lifeCircleStoreDO.setType(bean.getType());
        return lifeCircleStoreDO;
    }

}